package com.fita.admin.service.order.impl;

import com.demo2do.core.entity.exception.BusinessException;
import com.fita.admin.service.DataServiceImpl;
import com.fita.admin.service.order.EnquiryService;
import com.fita.entity.agent.Agent;
import com.fita.entity.order.reverse.Enquiry;
import com.fita.entity.order.reverse.ReverseOrder;
import com.fita.entity.principal.Principal;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @author: 11200
 * @date: 2019/8/26
 */
@Service("enquiryService")
public class EnquiryServiceImpl extends DataServiceImpl<Enquiry> implements EnquiryService {

    /**
     * 创建订单
     * @param enquiryId
     * @param loginId
     */
    public void createReverseOrder(Long enquiryId, Long loginId) {

        Principal principal = genericDaoSupport.load(Principal.class, loginId);

        Enquiry enquiry = load(Enquiry.class, enquiryId);

        if (enquiry == null){
            throw BusinessException.newInstance("500", "该询价单不存在");
        }
        ReverseOrder reverseOrder = new ReverseOrder(enquiry, principal);

        genericDaoSupport.save(reverseOrder);

        enquiry.ordered();

        genericDaoSupport.update(enquiry);
    }

    /**
     * 创建询价单
     *
     * @param enquiry
     * @return
     */
    public Enquiry create(Enquiry enquiry) {

        this.save(enquiry);

        return enquiry;
    }
}
